from flask_restful import Resource

from model.crud import QueryOneInstance, UpdateInstance
from model.tables import Blog, NowStr
from restful.data import DataPublish
from restful.error import RenderPostNotExist
from restful.response import ResponseSuccess
from restful.session import current_user_required,data


class Publish(Resource):
    @current_user_required
    @data(DataPublish())
    def post(self,data,current_user):
        if data['pid'] is None:
            blog=Blog(uid=current_user.id)
        else:
            blog=QueryOneInstance(Blog,{'id':data['pid']})
            if not blog:
                return RenderPostNotExist(data['pid'])
        blog.title=data['title']
        blog.content=data['content']
        blog.sort=data['sort']
        blog.time=NowStr()
        UpdateInstance(blog)
        return ResponseSuccess(msg="发布成功")

